import { Component, OnInit } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { UserService } from 'src/app/core/service/user.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-add-users-modal',
  templateUrl: './add-user-modal.component.html',
  styleUrls: ['./add-user-modal.component.scss']
})
export class AddUserModalComponent implements OnInit {
  usersForm: FormGroup;
  emailForm: FormGroup;
  passwordForm: FormGroup;
  info = {};
  defaultValue = '0';

  msgFlag = '';
  isRepeate = true;

  constructor(
    private modal: NzModalRef,
    private message: NzMessageService,
    private userService: UserService,
    fb: FormBuilder
  ) {
    this.usersForm = fb.group({
      email: [null, Validators.compose([Validators.required, Validators.email])],
      lastName: [null, Validators.compose([Validators.required])],
      firstName: [null, Validators.compose([Validators.required])],
      passwordCreationMode: ['0', Validators.compose([Validators.required])],
      ldapName: ['', Validators.compose([Validators.required])],
      newPassword: ['', Validators.compose([Validators.required, Validators.maxLength(12), Validators.minLength(8)])],
      confirmPwd: ['', Validators.compose([Validators.required, Validators.maxLength(12), Validators.minLength(8)])]
    });
  }

  destroyModal(): void {
    this.modal.destroy({ data: 'this the result data' });
  }

  ngOnInit() {
  }

  /**
   * 创建用户
   */
  saveUser() {
    console.log('提交', this.usersForm.controls);
    // tslint:disable-next-line:forin
    for (const c in this.usersForm.controls) {
      this.usersForm.controls[c].markAsTouched();
    }

    if (this.usersForm.controls.passwordCreationMode.value === '0') {
      this.usersForm.removeControl('ldapName');
      if (this.usersForm.valid) {
        this.info = {
          'createType': this.usersForm.controls.passwordCreationMode.value,
          'email': this.usersForm.controls.email.value,
          'firstName': this.usersForm.controls.firstName.value,
          'lastName': this.usersForm.controls.lastName.value,
          'password': this.usersForm.controls.newPassword.value,
          'passwordConfirm': this.usersForm.controls.confirmPwd.value
        };
        this.addService(this.info);
      } else {
        this.message.error('表单校验失败', { nzDuration: 10000 });
      }
    }
    if (this.usersForm.controls.passwordCreationMode.value === '1') {
      this.usersForm.removeControl('ldapName');
      this.usersForm.removeControl('newPassword');
      this.usersForm.removeControl('confirmPwd');

      // tslint:disable-next-line:forin
      for (const c in this.usersForm.controls) {
        this.usersForm.controls[c].markAsTouched();
      }
      if (this.usersForm.valid) {
        this.info = {
          'createType': this.usersForm.controls.passwordCreationMode.value,
          'email': this.usersForm.controls.email.value,
          'firstName': this.usersForm.controls.firstName.value,
          'lastName': this.usersForm.controls.lastName.value,
        };
        this.addService(this.info);
      } else {
        this.message.error('表单校验失败', { nzDuration: 10000 });
      }
    }
  }

  private addService(info: any) {
    this.userService.saveUser(this.info)
      .subscribe((saveUser) => {
        console.log(saveUser);
        if (saveUser.status) {
          this.message.success('添加成功!', {
            nzDuration: 10000
          });
          this.modal.destroy();
        } else {
          this.message.error('添加失败!请检查后重新填写', {
            nzDuration: 10000
          });
        }
      });
  }

  /**
   * 判断邮箱是否重复
   */
  isEmailRepeat() {
    console.log('=== Janine.isEmailRepeat().email ===>', this.usersForm.controls.email.value);
    if (this.usersForm.controls.email.value) {
      this.userService.isEmailRepeat(this.usersForm.controls.email.value)
        .subscribe((data) => {
          console.log(data);
          if (data.status === 3) { // 重复
            this.msgFlag = 'error';
          }
          if (data.status === 1) { // 可用
            this.msgFlag = 'success';
          }
        });
    }
  }

  pwdRepeate(): Boolean {
    const pwd = this.usersForm.controls.newPassword.value;
    const confirmPwd = this.usersForm.controls.confirmPwd.value;

    if (pwd === confirmPwd) {
      this.isRepeate = false;
      return this.isRepeate;
    } else {
      this.isRepeate = true;
      return this.isRepeate;
    }

  }

}
